<?php

namespace app\user\controller;

use app\user\controller\Auth;
use think\Db;

class Core extends Auth
{
    // 白名单
    protected $is_check_login = ['*'];

    // 入口
    public static function inlet($method = '')
    {
        return;
    }

    // 用户收藏记录
    public  function putCollection()
    {
        // 获取用户信息
        $userinfo = session('userinfo');
        // 是否批量删除
        if (!empty(input('id'))) {
            $id = input('id') == null ? '0' : input('id');
            $id = explode(',', $id);
            $res = Db::table('v1_collect')->delete($id);
            return false;
        }
        // 清空
        if (!empty(input('all')) && input('all') == 1) {
            $res = Db::table('v1_collect')->where('user_id', $userinfo['user_id'])->delete();
            return false;
        }
        // 初始化记录信息
        $info['collect_title'] = $this->hasValue(input('title'));
        $info['collect_pic'] = $this->hasValue(input('pic'));
        $info['collect_url'] =  $this->hasValue(input('url'));
        $info['collect_type'] = $this->hasValue(input('type'));
        $info['user_id'] = $userinfo['user_id'];
        // 查询历史记录
        $data = Db::table('v1_collect')->where('user_id', $info['user_id'])->where('collect_url', $info['collect_url'])->find();
        // 如果存在则删除 不然添加
        if ($data) {
            $type = false;
            $res = Db::table('v1_collect')->where('collect_id', $data['collect_id'])->delete();
        } else {
            $type = true;
            $res =  Db::table('v1_collect')->insert($info);
        }
        return $type;
    }

    // 用户播放记录
    public  function putSeeRecord()
    {
        // 获取用户信息
        $userinfo = session('userinfo');
        // 是否批量删除
        if (!empty(input('id'))) {
            $id = input('id') == null ? '0' : input('id');
            $id = explode(',', $id);
            $res = Db::table('v1_see_record')->delete($id);
            return false;
        }
        // 清空
        if (!empty(input('all')) && input('all') == 1) {
            $res = Db::table('v1_see_record')->where('user_id', $userinfo['user_id'])->delete();
            return false;
        }
        // 初始化记录信息
        $info['see_title'] = $this->hasValue(input('title'));
        $info['see_pic'] = $this->hasValue(input('pic'));
        $info['see_url'] =  $this->hasValue(input('url'));
        $info['see_type'] = $this->hasValue(input('type'));
        $info['user_id'] = $userinfo['user_id'];
        // 查询历史记录
        $data = Db::table('v1_see_record')->where('user_id', $info['user_id'])->where('see_url', $info['see_url'])->find();
        // 如果存在则更新 不然添加
        if ($data) {
            $type = true;
            $info['see_count'] = $data['see_count'] + 1;
            $res =  Db::table('v1_see_record')->where('see_id', $data['see_id'])->update($info);
        } else {
            $type = true;
            $res =  Db::table('v1_see_record')->insert($info);
        }
        return $type;
    }

    // 用户评论
    public function putComment()
    {
        $info = ['user_id' => input('uid'), 'say_type' => input('type'), 'say_content' => input('content')];
        // 添加用户
        $user_id =  Db::table('v1_user')->insertGetId($info);
    }
}
